//
//  HTSongViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTSongViewController.h"
#import "HTSongModel.h"
#import "PKRequestManager.h"
#import "HTSongCell.h"
#import "HTPlayerManager.h"
#import "HTPlayerViewController.h"
#import "HTPlayerManager.h"


@interface HTSongViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation HTSongViewController
- (NSMutableArray *)dataSourceArray {
    
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLabel.text = self.singer_name;
    
//    [self.rightButton setTitle:@"进入" forState:(UIControlStateNormal)];
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"正在播放"] forState:(UIControlStateNormal)];
    [self.rightButton addTarget:self action:@selector(handleIntoButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 150)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerClass:[HTSongCell class] forCellReuseIdentifier:@"cell"];
    _mainTableView.backgroundColor = [UIColor clearColor];
    _mainTableView.rowHeight = 50;

    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 120)];
    imageView.image = [UIImage imageNamed:@"海"];
    [self.view addSubview:imageView];
    [self.view addSubview:_mainTableView];
    [self configureData];
    
}

- (void)configureData {
    
    NSString *urlStr = [@"http://api.dongting.com/song/singer/" stringByAppendingFormat:@"%@%@", self.singer_id, @"/songs?page=1&size=50&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%252C2&f=f320&s=s310&imsi=&hid=&splus=8.1.2&active=1&net=2&openudid=6520cfef5c2a548f91e7a2ef2bbb4dcbd65c20a9&idfa=FE06BD4D-0645-40B5-9A51-27E13F83A35E&utdid=VdWOeXwUZRwDAA9QVJCvEyX9&alf=201200&bundle_id=com.ttpod.music"];
    [PKRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *modelDic in dict[@"data"]) {
            HTSongModel *model = [[HTSongModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HTSongCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HTSongModel *model = _dataSourceArray[indexPath.row];
    cell.nameLabel.text = model.name;
    cell.albumNameLabel.text = [@"《" stringByAppendingFormat:@"%@%@", model.albumName, @"》"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HTPlayerViewController *playerVC = [HTPlayerViewController sharePlayer];
    playerVC.songsArr = self.dataSourceArray;
    playerVC.selectedIndexPath = indexPath;
    [playerVC setPlayerInfo];
    [self presentViewController:playerVC animated:YES completion:^{
    }];
}

- (void)handleIntoButtonAction:(id)sender {
    
    if ([HTPlayerManager shareManager].avPlayer.rate == 1) {
        HTPlayerViewController *playerVC = [HTPlayerViewController sharePlayer];
        [self presentViewController:playerVC animated:YES completion:^{
        }];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.layer.transform = CATransform3DMakeScale(1, 0, 1);
    [UIView animateWithDuration:1 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}

- (void)handleReturnButtonAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
